<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* wish 产品上传model
*/
class Wishproduct_m extends CI_Model
{
	
	function __construct()
	{
		  parent::__construct();
		 
	}

	public function upload($uploadjson){
		$upload=json_decode($uploadjson,true);

		//对第一行进行判断，确定有哪些参数
        $into=array();
		$title=array();
		foreach ($upload[0] as $upv) {

			if (strpos($upv,'*')===false) {
			    $up=$upv;
			}else{
				$up=substr($upv,1);
			}
            if(strpos($upv,'(')===false){

            }else{
                $up=substr($upv,0,strpos($upv,'('));
            }
            $title[]=str_replace(' ', '_',$up);
		}

		$num=16;
		 $lnum=$num;
        //已知所有key值
        $j=0;
        foreach ($upload as $value) {
             
        	if ($j==0) {
        		$j++;
        		continue;
        	}
        $into['wish_id']=null;
        $into['Parent_Unique_ID']='';
		$into['Unique_ID']='';
		$into['UPC']='';
		$into['Merchant_Name']='';
		$into['Product_Name']='';
		$into['Color']='';
		$into['Size']='';
		$into['Quantity']='';
		$into['Tags']='';
		$into['Description']='';
		$into['MSRP']='';
		$into['Price']='';
		$into['Shipping']='';
		$into['Product_Page']='';
		$into['Main_Image_URL']='';
		$into['Extra_Image_URL']='';
		$into['updatatime']='';
		$into['user']='';
        $into['Shipping_Time']='';
        // print_r($value);
        //        echo "<br/>";
        	for ($i=0; $i < $lnum ; $i++) { 
        		$into[$title[$i]]=serialize($value[$i]);
                // print_r($title[$i]);
                // echo "<br/>";
                // print_r($value[$i]);
                // echo "<br/>";
        	}

        	// $Extra_Image_URL=array();
            // for ($k=$lnum; $k <$num ; $k++) { 
            // 	if (empty($value[$i])) {
            // 		continue;
            // 	}
            //    $Extra_Image_URL[]=serialize($value[$k]);
            // }
            // $Extra_Image_URLjson=serialize($Extra_Image_URL);
            // $into['Extra_Image_URL']=$Extra_Image_URLjson;
            $into['updatatime']=date('Y-m-d H:i:s');
            $into['user']=$_COOKIE["username"];
            
            $Unique_ID=$into['Unique_ID'];
            //判断是否存在相同的sku，存在的话，本行数据跳过
            $sql="select * from db_wishproduct where Unique_ID='{$Unique_ID}';";
            $arr=$this->db->query($sql)->result_array();
            if (!empty($arr)) {
               continue;
            }
        // exit;
              // $sql='insert into db_wishproduct values(null,"'.$into['Parent_Unique_ID'].'","'.$into['Unique_ID'].'","'.$into['UPC'].'","'.$into['Merchant_Name'].'","'.$into['Product_Name'].'","'.$into['Color'].'","'.$into['Size'].'","'.$into['Quantity'].'","'.$into['Tags'].'","'.$into['Description'].'","'.$into['MSRP'].'","'.$into['Price'].'","'.$into['Shipping'].'","'.$into['Product_Page'].'","'.$into['Main_Image_URL'].'","'.$into['Extra_Image_URL'].'","'.$into['updatatime'].'","'.$into['user'].'","'.$into['Shipping_Time'].'");';
             $this->db->insert("db_wishproduct",$into);
            // $this->db->query($sql);
           
        $j++;
        }


	}

    public function displaytpl($id=0,$search='*'){
        if ($id==0) {
           $sql="select * from db_wishproduct order by wish_id desc limit 0,200;";
           if ($search!='*') {

               $sql="select * from db_wishproduct where Parent_Unique_ID like'%{$search}%' order by wish_id desc limit 0,200;";
               
           }
        }else{
            $sql="select * from db_wishproduct where wish_id='{$id}';";
        }
        $arr=$this->db->query($sql)->result_array();
         foreach ($arr as $key => $value) {
        $arr[$key]['Parent_Unique_ID']=unserialize($value['Parent_Unique_ID']);
        $arr[$key]['Unique_ID']=unserialize($value['Unique_ID']);
        $arr[$key]['UPC']=unserialize($value['UPC']);
        $arr[$key]['Merchant_Name']=unserialize($value['Merchant_Name']);
        if ($id==0) {
        $arr[$key]['Product_Name']=substr(unserialize($value['Product_Name']),0,20);
        }else{
        $arr[$key]['Product_Name']=unserialize($value['Product_Name']);            
        }
        $arr[$key]['Color']=unserialize($value['Color']);
        $arr[$key]['Size']=unserialize($value['Size']);
        $arr[$key]['Quantity']=unserialize($value['Quantity']);
        $arr[$key]['Tags']=unserialize($value['Tags']);
        $arr[$key]['Description']=unserialize($value['Description']);
        $arr[$key]['MSRP']=unserialize($value['MSRP']);
        $arr[$key]['Price']=unserialize($value['Price']);
        $arr[$key]['Shipping']=unserialize($value['Shipping']);
        $arr[$key]['Product_Page']=unserialize($value['Product_Page']);
        $arr[$key]['Main_Image_URL']=unserialize($value['Main_Image_URL']);
        $arr[$key]['Extra_Image_URL']=unserialize($value['Extra_Image_URL']);
        $arr[$key]['Shipping_Time']=unserialize($value['Shipping_Time']);

         }
       
        return $arr;
    }
    
    public function selectshop(){
    	$sql="select * from d_shop where platfromname ='Wish';";
    	$shop=$this->db->query($sql)->result_array();
    	$str='<ul>';
    	foreach ($shop as $sv) {
    		$oneshop=json_decode($sv['amazonkey'],true);
    	 if (!isset($oneshop['wishkey'])) 	continue;
            
         $str.="<li><input name='shopid' type='checkbox' class='none' value='{$sv['id']}' />{$sv['name']}</li>";
    	}
    	$str.="</ul>";
         print_r($str);
        // die;
    	// return $str;
    }
    


    public function saveupload($productid,$shopid){
        $this->load->library('Amazondata');
        $productarr=explode(',',$productid);
        $shoparr=explode(',',$shopid);
        $time=date('Y-m-d H:i:s');
        $user=$_COOKIE["username"];
        foreach ($productarr as $pv) {
            foreach ($shoparr as $sv) {
               $sql="insert into db_wish_to_platform values(null,'{$pv}','{$sv}','{$time}','{$user}','0','0','');";
               $this->db->query($sql);
               $wpid=$this->db->insert_id();
       //同步到远程
               $sql2="select * from d_shop where agent_visit='1' and id='{$sv}';";
               $num=$this->db->query($sql2)->result_array();
               if(!empty($num)){
                $this->amazondata->wishproduct($pv,$sv,$time,$user,$wpid);
               }
        


            }
        }



        

        
    }

    public function saveproduct($intojson){
        $into=json_decode($intojson,true);
        $inarr=array();
        foreach ($into as $key => $value) {
            $inarr[$key]=serialize($value);
        }
        $inarr['updatatime']=date('Y-m-d H:i:s');
        $inarr['user']=$_COOKIE["username"];
        $this->db->insert("db_wishproduct",$inarr);
        
    }

    
    public function updata($intojson,$id){
        $into=json_decode($intojson,true);
        $inarr=array();
        foreach ($into as $key => $value) {
            $inarr[$key]=serialize($value);
        }

      $this->db->update('db_wishproduct',$inarr,"wish_id='".$id."'");
    }

    public function delect($id){
        $idarr=explode(',', $id);
        foreach ($idarr as $value) {
            $sql="DELETE FROM db_wishproduct where wish_id='{$value}';";
            $this->db->query($sql);
        }
    }

   public function status($value='*'){
   //显示商品上传状态
    if ($value=='*') {
          $sql="select wish.*,shop.name,wp.Unique_ID from db_wish_to_platform as wish ,d_shop as shop,db_wishproduct as wp where wish.shop_id= shop.id and wish.product_id=wp.wish_id order by wish.wp_id desc limit 0,500;";
    }else{
            $sql="select wish.*,shop.name,wp.Unique_ID from db_wish_to_platform as wish ,d_shop as shop,db_wishproduct as wp where wish.shop_id= shop.id and wish.product_id=wp.wish_id and wp.Parent_Unique_ID like '%{$value}%' order by wish.wp_id desc;";
    }

    $arr=$this->db->query($sql)->result_array();
    $array=array();
    foreach ($arr as $v) {
        $logarr=unserialize($v['log']);
        $Unique_ID=unserialize($v['Unique_ID']);
        if ($logarr['code']==0) {
           $v['log']='ok';
        }else{
            $v['log']=$logarr['message'];
        }
       $v['Unique_ID']=$Unique_ID;
       $array[]=$v;
    }
    return $array;
   }

  
}
?>